/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * This class is a JPA entity and is used to represent a person
 * as it will be stored in the database
 * 
 * @author Amigos do Noddy
 *
 */
@Entity
@Table(name = "PERSON", schema = "APP")
@NamedQueries({@NamedQuery(name = "Person.findByIdperson", query = "SELECT p FROM Person p WHERE p.idperson = :idperson"), @NamedQuery(name = "Person.findByName", query = "SELECT p FROM Person p WHERE p.name = :name"), @NamedQuery(name = "Person.findByUsername", query = "SELECT p FROM Person p WHERE p.username = :username"), @NamedQuery(name = "Person.findByPassword", query = "SELECT p FROM Person p WHERE p.password = :password"), @NamedQuery(name = "Person.findByEmail", query = "SELECT p FROM Person p WHERE p.email = :email"), @NamedQuery(name = "Person.findByDate", query = "SELECT p FROM Person p WHERE p.date = :date"), @NamedQuery(name = "Person.findByActive", query = "SELECT p FROM Person p WHERE p.active = :active")})
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="PersonType", discriminatorType=DiscriminatorType.STRING)
public abstract class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "IDPERSON", nullable = false)
    private Integer idperson;
    
	@Column(name = "PersonType")        
	private String PersonType;
    
    @Column(name = "NAME", nullable = false)
    private String name;
    @Column(name = "USERNAME", nullable = false)
    private String username;
    @Column(name = "PASSWORD", nullable = false)
    private String password;
    @Column(name = "EMAIL", nullable = false)
    private String email;
    @Column(name = "DATE", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date date;
    @Column(name = "ACTIVE", nullable = false)
    private short active;
    
    /*@OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
    
    private Assistant assistant;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
    private Administrator administrator;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "refidpersonAuthor")
    private Collection<Comment> commentCollection;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
    private Utilizador utilizador;*/

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "refidpersonAuthor")
    private Collection<Comment> commentCollection;
    
    /**
     * Default constructor
     */
    public Person() {
    }

    /*public Person(Integer idperson) {
        this.idperson = idperson;
    }*/

    /**
     * Class constructor
     * 
     * @param name name
     * @param username username
     * @param password password
     * @param email email
     * @param date date of admission in the system
     */
    public Person(/*Integer idperson,*/ String name, String username, String password, String email, Date date) {
        //this.idperson = idperson;
        this.name = name;
        this.username = username;
        this.password = password;
        this.email = email;
        this.date = date;
        this.active = 1;
    }

    /**
     * Gets the id of the person
     * @return person id
     */
    public Integer getIdperson() {
        return idperson;
    }

    /*public void setIdperson(Integer idperson) {
        this.idperson = idperson;
    }*/

    /**
     * Gets the name of the person
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the person
     * @param name person's name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the username of the person
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * Sets the username of the person
     * @param username username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Gets the password of the person
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Sets the password of the person
     * @param password password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Gets the email of the person
     * @return email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets the email of the person
     * @param email email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Gets the date of admission in the system
     * @return date of admission
     */
    public Date getDate() {
        return date;
    }

    /**
     * Sets the date of admission in the system
     * @param date date of admission
     */
    public void setDate(Date date) {
        this.date = date;
    }

    /**
     * Gets the active state of the person
     * @return active state
     */
    public short getActive() {
        return active;
    }

    /**
     * Sets the active state of the person
     * @param active active state
     */
    public void setActive(short active) {
        this.active = active;
    }
/*
    public Assistant getAssistant() {
        return assistant;
    }

    public void setAssistant(Assistant assistant) {
        this.assistant = assistant;
    }

    public Administrator getAdministrator() {
        return administrator;
    }

    public void setAdministrator(Administrator administrator) {
        this.administrator = administrator;
    }

    public Collection<Comment> getCommentCollection() {
        return commentCollection;
    }

    public void setCommentCollection(Collection<Comment> commentCollection) {
        this.commentCollection = commentCollection;
    }

    public Utilizador getUtilizador() {
        return utilizador;
    }

    public void setUtilizador(Utilizador utilizador) {
        this.utilizador = utilizador;
    }
*/
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idperson != null ? idperson.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Person)) {
            return false;
        }
        Person other = (Person) object;
        if ((this.idperson == null && other.idperson != null) || (this.idperson != null && !this.idperson.equals(other.idperson))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "persistence.Person[idperson=" + idperson + "]";
    }

	public void setCommentCollection(Collection<Comment> commentCollection) {
		this.commentCollection = commentCollection;
	}

	public Collection<Comment> getCommentCollection() {
		return commentCollection;
	}

	public void setPersonType(String personType) {
		PersonType = personType;
	}

	public String getPersonType() {
		return PersonType;
	}

}
